package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.dao.CommentMapper;
import com.bbt.dormassistant.entity.Comment;
import com.bbt.dormassistant.entity.CommentDetail;
import com.bbt.dormassistant.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    CommentMapper commentMapper;


    /**
     *
     * @param userId 用户ID
     * @param messageId 文章ID
     * @param text 文章内容
     * @return Boolean
     */
    @Override
    public Boolean addComment(String userId, Integer messageId, String text) {
        try{
            Comment comment = new Comment();
            comment.setUserId(userId);
            comment.setMessageId(messageId);
            comment.setText(text);
            comment.setTime(new Date());
            return commentMapper.insert(comment) == 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }


    /**
     *
     * @param messageId 需要查询评论的文章ID
     * @return List<CommentDetail>
     */
    @Override
    public List<CommentDetail> queryAllByMessageId(Integer messageId) {
        return commentMapper.selectByMessageId(messageId);
    }


    /**
     *
     * @param currIndex 当前页
     * @param pageSize 页大小
     * @return List<Comment>
     */
    @Override
    public List<Comment> queryAll(Integer currIndex, Integer pageSize) {
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",currIndex==null? 0:(currIndex-1)*pageSize );
        data.put("pageSize",pageSize==null? 1001:pageSize);
        return commentMapper.selectAll(data);
    }


}
